package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.minio.MinIoTemplate;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import com.heima.wemedia.thread.WmUserThreadLocal;
import javafx.scene.paint.Material;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.multipart.MultipartFile;

import javax.swing.*;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;


@Slf4j
@Service
@Transactional
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {


    @Autowired
    private MinIoTemplate minIoTemplate;

    @Autowired
    private WmMaterialMapper wmMaterialMapper;

    @Override
    public ResponseResult load(MultipartFile multipartFile) throws IOException {
        //得到一个流
        InputStream inputStream = multipartFile.getInputStream();
        if (inputStream==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //获得图片类型
        String contentType = multipartFile.getContentType();
        String[] split = contentType.split("/");
        String suffName = split[1];
        String fineName=System.currentTimeMillis()+"."+suffName;
        //用minio将流上传返回url地址
        String url = minIoTemplate.uploadFile("", fineName, inputStream, "images");
        WmMaterial material=new WmMaterial();
        material.setCreatedTime(new Date());
        material.setUserId(WmUserThreadLocal.get().getId());
        material.setType((short) 0);
        material.setIsCollection((short) 0);
        material.setUrl(url);
        int i = wmMaterialMapper.insert(material);
        if (i<1){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        return ResponseResult.okResult(material);
    }

    @Override
    public PageResponseResult findAll(WmMaterialDto dto) {
        LambdaQueryWrapper<WmMaterial> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmMaterial::getIsCollection,dto.getIsCollection());
        Page<WmMaterial> page = new Page<>(dto.getPage(),dto.getSize());
        page = wmMaterialMapper.selectPage(page, wrapper);
        Long total = page.getTotal();
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(),dto.getSize(),total.intValue());
        pageResponseResult.setData(page.getRecords());
        return pageResponseResult;
    }
}
